package com.lms.logisticsmanagementsystem.controller;

import com.lms.logisticsmanagementsystem.model.Car;
import com.lms.logisticsmanagementsystem.model.Driver;
import com.lms.logisticsmanagementsystem.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/car")
public class CarController {

    @Autowired
    private CarService carService;

    private Map<String, Object> tableFormat(Integer page, Integer limit, List list) {
        Map<String, Object> map = new HashMap<>();
        List curList = new ArrayList();
        //当前数据索引
        Integer curIndex = (page - 1) * limit;
        //目标数据索引
        Integer tarIndex = curIndex + limit;
        //数据总数
        Integer total = list.size();
        //如果目标行索引大于数据总数，则返回数据总数进行判断
        if (tarIndex > total) {
            tarIndex = total;
        }
        for (int i = curIndex; i < tarIndex; i++) {
            curList.add(list.get(i));
        }
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", list.size());
        map.put("data", curList);
        return map;
    }

    @GetMapping("/carInfo")
    public String carInfo() {
        return "car/carInfo";
    }

    @GetMapping("/addCarInfo")
    public String addCarInfo() {
        return "car/addCarInfo";
    }

    @GetMapping("/editCarInfo")
    public String editCar(@RequestParam("id") String id, Model model) {
        //获取编辑信息
        Car car = carService.getCarInfoById(Integer.valueOf(id));
        model.addAttribute("car", car);
        return "car/editCarInfo";
    }

    @PostMapping("/getCarInfo")
    @ResponseBody
    public Map<String, Object> getCarInfo(HttpServletRequest request) {
        String carNum = request.getParameter("carNum");
        //获取表格分页条件
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        Integer p = Integer.valueOf(page);
        Integer l = Integer.valueOf(limit);
        //获取后台数据
        List<Car> list = new ArrayList<>();
        if (carNum==null||carNum==""){
            list = carService.getCarInfo();
        }else {
            list = carService.getCarInfoByCarNum(carNum);
        }
        Map<String, Object> map = new HashMap<>();
        map = tableFormat(p, l, list);
        return map;
    }

    @PostMapping("/addCar")
    @ResponseBody
    public String addCar(@RequestBody Car car) {
        //添加车辆编号
        if (car.getCBH() == null || car.getCBH() == "") {
            Date cd = new Date();
            SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
            String CBH = f.format(cd);
            for (int i = 0; i < 3; i++) {
                int extra = new Random().nextInt(10);
                CBH += String.valueOf(extra);
            }
            car.setCBH(CBH);
        }
        //
        boolean flag = carService.addCarInfo(car);
        if (flag) {
            return "success";
        } else {
            return "fail";
        }
    }

    @PostMapping("/editCar")
    @ResponseBody
    public String editCar(@RequestBody Car car) {
        boolean flag = carService.editCar(car);
        if (flag){
            return "success";
        }
        else return "fail";
    }

    @PostMapping("/delCarInfo")
    @ResponseBody
    public String delCarInfo(@RequestParam("id") String id) {
        System.out.println(id);
        Integer d = Integer.valueOf(id);
        boolean flag = carService.delCarInfoById(d);
        if (flag) {
            return "success";
        } else return "fail";
    }

    @GetMapping("/driverInfo")
    public String driverInfo() {
        return "car/driverInfo";
    }

    @GetMapping("/addDriverInfo")
    public String addDriverInfo(Model model) {
        //获取车牌号码
        List<String> cphm = carService.getCphmUnSelect();
        System.out.println(cphm);
        model.addAttribute("cphm", cphm);
        return "car/addDriverInfo";
    }

    @PostMapping("/addDriver")
    @ResponseBody
    public String addDriver(@RequestBody Driver driver) {
        boolean flag = carService.addDriverInfo(driver);
        if (flag) {
            return "success";
        } else return "fail";
    }

    @PostMapping("/getDriverInfo")
    @ResponseBody
    public Map<String, Object> getDriverInfo(HttpServletRequest request) {
        String drivertip = request.getParameter("drivertip");
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        Integer p = Integer.valueOf(page);
        Integer l = Integer.valueOf(limit);
        List<Driver> list=new ArrayList<>();
        if (drivertip==null || drivertip==""){
            list = carService.getDriverInfo();
        }
        else {
            list=carService.getDriverInfoByDriverTip(drivertip);
        }
        Map<String, Object> map = tableFormat(p, l, list);
        return map;
    }

    @GetMapping("/editDriverInfo")
    public String editDriverInfo(@RequestParam("id") String id, Model model) {
        Integer l = Integer.valueOf(id);
        //获取待编辑信息
        Driver driver = carService.getDriverInfoById(l);
        System.out.println(driver);
        List<String> cphm = carService.getCphmUnSelect();
        model.addAttribute("driver", driver);
        model.addAttribute("cphm", cphm);
        return "car/editDriverInfo";
    }

    @PostMapping("/editDriver")
    @ResponseBody
    public String editDriver(@RequestBody Driver driver) {
        System.out.println(driver);
        boolean flag = carService.editDriverById(driver);
        if (flag) {
            return "success";
        } else return "fail";
    }

    @PostMapping("/delDriverInfo")
    @ResponseBody
    public String delDriverInfo(HttpServletRequest request) {
        String id = request.getParameter("id");
        Integer l = Integer.valueOf(id);
        boolean flag = carService.delDriverInfoById(l);
        if (flag) {
            return "success";
        } else {
            return "fail";
        }
    }
}
